package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

public class MemberRepository {

    private PrintStream printer;
    private BufferedReader reader;
    private Map<String, String> loginInfo;

    public MemberRepository(PrintStream printer, BufferedReader reader) {
        this.printer = printer;
        this.reader = reader;
        initializeLogins();
    }

    private void initializeLogins(){
        loginInfo = new HashMap<String, String>();
        loginInfo.put("111-1111", "password");
        loginInfo.put("111-1112", "password");
        loginInfo.put("111-1113", "password");

    }
    public boolean validateUser(String username, String password) {
        boolean loginStatus = false;
        String userPassword = loginInfo.get(username);
        if (userPassword != null){
            if(userPassword.equals(password)) {
                loginStatus = true;
            }
        }
        return loginStatus;
    }

    public String login(String loginType) throws IOException {
        String loginStatus;
        if(loginType.equals("1")){
            loginStatus = "Guest";
        }
        else if(loginType.equals("2")){
            printer.println("Please enter your username");
            String username = reader.readLine();
            printer.println("Please enter your password");
            String password = reader.readLine();
            boolean loginSuccess = validateUser(username, password);
            if(loginSuccess){
                loginStatus = "Member";
            }else{
                loginStatus = "Invalid";
            }
        }else{
            loginStatus = "Invalid";
        }
        return loginStatus;
    }
}
